<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/11/17
 * Time: 11:01
 */

require('lib/Spreadsheet_Excel_Reader.class.php');

class Utils
{

    public static function getMsgByCode($code)
    {
        $array = array(1001 => "表單參數缺少", 1002 => "郵箱格式錯誤", 1003 => "IP地址錯誤", 1004 => "信用卡號錯誤", 1005 => "商戶號不存在", 1006 => "網址不存在", 1007 => "卡種不存在", 1008 => "簽名數據錯誤", 1009 => "貨幣代碼錯誤", 1010 => "國家代碼錯誤", 1012=>"同一卡號或郵箱，24小時內交易超限",2001 => "系統故障");
        if(!$array[$code]){
            return "支付错误";
        }
        return $array[$code];
    }

    public static function readXLS($file)
    {
        $data = new Spreadsheet_Excel_Reader();
        $data->setOutputEncoding('UTF-8'); //设置输出的编码为utf8
        $ret = $data->read($file); //要读取的excel文件地址
        if ($ret == -1) {
            $array = false;
        } else {
            for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
                for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
                    $array[$i - 1][$j - 1] = $data->sheets[0]['cells'][$i][$j];
                }
            }
        }
        return $array;
    }


    public static function parseHtml($file)
    {
        $real_html = file_get_contents($file);
        $matchall = array();
        $preg = '/<p class="whs14".*?>.*?<\/td>/';
        preg_match_all($preg, $real_html, $matchall);
        $currency_array = $matchall[0];
        return $currency_array;
    }


    public static function readCsv($filename)
    {
        $file = BASE_CONFIG . '/csv/' . $filename;
        if (($handle = fopen($file, "r")) !== FALSE) {
            while (($data = fgetcsv($handle, ",")) !== FALSE) {
                $res[] = $data;
            }
        }

        return $res;
    }


    public static function checkServer($url)
    {

        $context = array();
        $context ['http'] = array(
            'timeout' => 10,
            'method' => 'POST',
            'content' => ''
        );


        return file_get_contents($url, false, stream_context_create($context));

    }

    public static function httpPost($param)
    {
        if (empty($param)) {
            return false;
        }

        $url = "";
        $ret = self::checkServer(API_HOST1);
        if ($ret) {
            $url = API_HOST1;
        } else {
            $url = API_HOST2;
        }

        $postdata = http_build_query($param);

        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }


    public static function createLog($content, $file = '')
    {
        if (!$file) {
            $logfile = dirname(__FILE__) . '/logs/debuglog' . date('Ymd') . '.txt';
        } else {
            $logfile = $file;
        }

        if (!file_exists(dirname($logfile))) {
            @mkdir(dirname($logfile), 0777, true);
        }
        error_log(date("[Y-m-d H:i:s]") . " -[" . $_SERVER['REQUEST_URI'] . "] :" . $content . "\n", 3, $logfile);
    }


    public static function getValue($key, $filename)
    {
        $rt = '';
        $csvArray = self::getCsv($filename);
        foreach ($csvArray as $k => $v) {
            if ($key == $v[0]) {
                $rt = $v[1];
                break;
            }
        }

        return $rt;
    }

    public static function getCsv($filename)
    {
        setlocale(LC_ALL, 'zh_CN');
        $path = $filename;
        $file = fopen($path, 'r');
        while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容
            $lists[] = $data;
        }

        return $lists;
        fclose($file);
    }
}